//
// Created by liushuai on 2021/1/11.
//

#include "detectCycle.h"

ListNode *detectCycle(ListNode *head) {
  ListNode *fast = head;
  ListNode *slow = head;
  while (fast && fast->next && fast->next) {
    fast = fast->next->next;
    slow = slow->next;
    if (fast == slow) {
      fast = head;
      while (fast != slow) {
        fast = fast->next;
        slow = slow->next;
      }
      return fast;
    }
  }
  return nullptr;
}